using System;
using System.Collections.Generic;
using System.Text;

namespace Sio.Mdm.Types
{
    /// <summary>
    /// Basic information on user's session
    /// </summary>
    [Serializable]
    public class Session
    {
        #region Fields
        /// <summary>
        /// Get empty instance of session
        /// </summary>
        public static readonly Session Empty = new Session(0, Guid.Empty, String.Empty);
        #endregion

        #region Construction
        /// <summary>
        /// COnstruct session object
        /// </summary>
        /// <param name="idmStevilka">ID of user that owns session</param>
        /// <param name="sessionID">ID of session</param>
        /// <param name="uporabniskoIme">User name</param>
        /// 
        private Session(int idmStevilka, Guid sessionID, string uporabniskoIme)
        {
            _idmStevilka = idmStevilka;
            _sessionID = sessionID;
            _uporabniskoIme = uporabniskoIme;
        }

        public Session(int idmStevilka, Guid sessionID, string uporabniskoIme, int organizacijaID, string ime, string priimek, string organizacijaNaziv, int vlogaId, int tipVlogeId)
        {
            _idmStevilka = idmStevilka;
            _sessionID = sessionID;
            _uporabniskoIme = uporabniskoIme;
            _organizacijaID = organizacijaID;
            _ime = ime;
            _priimek = priimek; 
            _organizacijaNaziv = organizacijaNaziv;
            _vlogaId = vlogaId;
            _tipVlogeId = tipVlogeId;
        }
        #endregion

        #region Properties

        #region Public

        #region IdmStevilka
        private int _idmStevilka = 0;
        public int IdmStevilka
        {
            get
            {
                return _idmStevilka;
            }
        }
        #endregion


        #region Vloga
        private int _vlogaId = 0;
        public int VlogaId
        {
            get
            {
                return _vlogaId;
            }
        }
        #endregion

        #region TipVlogeId
        private int _tipVlogeId = 0;
        public int TipVlogeId
        {
            get
            {
                return _tipVlogeId;
            }
        }
        #endregion

        #region SuperAdmin
        private bool _superAdmin = true;
        public bool SuperAdmin
        {
            get
            {
                return IdmStevilka == 0;
            }
        }
        #endregion


        #region SessionID
        private Guid _sessionID;
        public Guid SessionID
        {
            get
            {
                return _sessionID;
            }
        }

        #endregion

        #region UserName
        private string _uporabniskoIme;
        public string UporabniskoIme
        {
            get
            {
                return _uporabniskoIme;
            }
        }

        private string _ime;
        public string Ime
        {
            get
            {
                return _ime;
            }
        }

        private string _priimek;
        public string Priimek
        {
            get
            {
                return _priimek;
            }
        }
        #endregion


        #region Organizacija
        private int _organizacijaID;
        public int Organizacija
        {
            get
            {
                return _organizacijaID;
            }
        }

        private string _organizacijaNaziv;
        public string OrganizacijaNaziv
        {
            get
            {
                return _organizacijaNaziv;
            }
        }
        #endregion


        #region UserData
        private Dictionary<string, object> _userData;
        /// <summary>
        /// Additional data bound to user session. Based on key and value both of string
        /// type. Usefull to promote additional parameters to other levels (BF, BL or DAL)
        /// </summary>
        public Dictionary<string, object> UserData
        {
            get
            {
                if (_userData == null)
                {
                    _userData = new Dictionary<string, object>();
                }

                return _userData;
            }
        }
        #endregion

        #region Current
        /// <summary>
        /// Get instance of current session
        /// </summary>
        public static Session Current
        {
            get
            {
                return SessionFactory.Session;
            }
        }
        #endregion

        #endregion

        #endregion

        #region Methods

        #region Public

        #region IsEmpty
        /// <summary>
        /// Check wheater instance is empty session
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return SessionID == Guid.Empty && IdmStevilka == 0 && UporabniskoIme == String.Empty;
        }
        #endregion

        #region GetUserData
        /// <summary>
        /// Get specified value of key stored in user specific data
        /// </summary>
        /// <typeparam name="type">Type to cast value into</typeparam>
        /// <param name="userSessionKey">Key used for stored data</param>
        /// <returns>Value of stored data in specified type</returns>
        public type GetUserData<type>(string userSessionKey)
        {
            return (type)UserData[userSessionKey];
        }
        #endregion

        #region ToString
        /// <summary>
        /// Overriden to show user friendlydata on session
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return String.Format("User={0}, SessionId={1}", UporabniskoIme, SessionID);
        } 
        #endregion

        #endregion

        #endregion
    }
}
